#include "cv.h"
#include "highgui.h"
#include <string>
using namespace std;

/**
 * color2gray
 * @param: string filePath : input video file
 *         string outPath : name of new output file
 * @return: 
 */
void color2gray(string filePath, string outName){
	//load vedio file
	CvCapture* capture = 0;
	capture = cvCreateFileCapture(filePath.data());
	if (!capture) {
		return;
	}

	IplImage *bgrFrame = cvQueryFrame(capture);//init the video read
	double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
	CvSize size = cvSize((int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));
	CvVideoWriter* writer = cvCreateVideoWriter(outName.data(), CV_FOURCC('M','J','P','G'),fps,size);
	IplImage* logPolarFrame = cvCreateImage(size, IPL_DEPTH_8U, 3);

	while ((bgrFrame = cvQueryFrame(capture)) != NULL) {
		cvLogPolar(bgrFrame, logPolarFrame, cvPoint2D32f(bgrFrame->width/2, bgrFrame->height/2), 40, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS);
		cvWriteFrame(writer, logPolarFrame);
	}

	cvReleaseVideoWriter(&writer);
	cvReleaseImage(&logPolarFrame);
	cvReleaseCapture(&capture);
}

/**
 * main
 * @param: 
 * @return: 
 */
void main(){
	string filePath = "chn.avi";
	string outName = "chn_gray.avi";
	color2gray(filePath, outName);
	printf("succeed");
	cvWaitKey(0);
}